package com.soduko.digitpopup;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class digitPopup extends Activity {
	
	PopupWindow digitPopup;
	Button playfield_btn1, playfield_btn2, playfield_btn3, playfield_btn4, playfield_btn5, playfield_btn6, playfield_btn7, playfield_btn8, playfield_btn9;
	Button digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, digit_8, digit_9, digit_blank;
	LinearLayout digitHolder;
	Button temp, chosenBtn;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //create main
        playfield_btn1 = (Button)findViewById(R.id.main_button1);
        playfield_btn1.setOnClickListener(new clickHandler());
        playfield_btn2 = (Button)findViewById(R.id.main_button2);
        playfield_btn2.setOnClickListener(new clickHandler());
        playfield_btn3 = (Button)findViewById(R.id.main_button3);
        playfield_btn3.setOnClickListener(new clickHandler());
        playfield_btn4 = (Button)findViewById(R.id.main_button4);
        playfield_btn4.setOnClickListener(new clickHandler());
        playfield_btn5 = (Button)findViewById(R.id.main_button5);
        playfield_btn5.setOnClickListener(new clickHandler());
        playfield_btn6 = (Button)findViewById(R.id.main_button6);
        playfield_btn6.setOnClickListener(new clickHandler());
        playfield_btn7 = (Button)findViewById(R.id.main_button7);
        playfield_btn7.setOnClickListener(new clickHandler());
        playfield_btn8 = (Button)findViewById(R.id.main_button8);
        playfield_btn8.setOnClickListener(new clickHandler());
        playfield_btn9 = (Button)findViewById(R.id.main_button9);
        playfield_btn9.setOnClickListener(new clickHandler());
        
        //create popup
        digitPopup = new PopupWindow(View.inflate(this, R.layout.digitpopup, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
        digitHolder = (LinearLayout)digitPopup.getContentView().findViewById(R.id.digitholder);
        digitHolder.setOnClickListener(new clickHandler());
        digit_1 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_1);
        digit_1.setOnClickListener(new clickHandler());
        digit_2 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_2);
        digit_2.setOnClickListener(new clickHandler());
        digit_3 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_3);
        digit_3.setOnClickListener(new clickHandler());
        digit_4 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_4);
        digit_4.setOnClickListener(new clickHandler());
        digit_5 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_5);
        digit_5.setOnClickListener(new clickHandler());
        digit_6 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_6);
        digit_6.setOnClickListener(new clickHandler());
        digit_7 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_7);
        digit_7.setOnClickListener(new clickHandler());
        digit_8 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_8);
        digit_8.setOnClickListener(new clickHandler());
        digit_9 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_9);
        digit_9.setOnClickListener(new clickHandler());
        digit_blank = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_blank);
        digit_blank.setOnClickListener(new clickHandler());
    }
    
  //show or stop showing a window
    public void toggleWindow(PopupWindow win){  	
    	    	
    	if(!win.isShowing()){
    		/*changes the position of digitholder to the 
        	position of the pushed button*/
        	if(win==digitPopup){
        		int[] loc = {0,0};
        		chosenBtn.getLocationOnScreen(loc);
        		digitPopup.getContentView().findViewById(R.id.digitholder).scrollBy(-loc[0]+35, -loc[1]+60);
        	}
        	
    		win.showAtLocation(this.findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);
    	}
    	else{    		
    		win.dismiss();
    		
    		/*resets position (otherwise it will continue out 
    		of more and more each time it's used)*/
    		if(win==digitPopup){
        		int[] loc = {0,0};
        		chosenBtn.getLocationOnScreen(loc);
        		digitPopup.getContentView().findViewById(R.id.digitholder).scrollBy(loc[0]-35, loc[1]-60);
    		}
    	}
    }
    
    
  //subclass for handling button presses
    class  clickHandler implements Button.OnClickListener{              
    	
    	@Override
    	public void onClick(View v){
    		//press a playfield button
	    	if(v.getParent().getParent()==findViewById(R.id.playfield)){
	    		chosenBtn = (Button)v;
	    		toggleWindow(digitPopup);
	    	}
	    	//insert number
	    	else if(v.getParent().getParent()==digitHolder){
	    		temp = (Button)v;
	    		chosenBtn.setText(temp.getText());
	    		toggleWindow(digitPopup);
	    	}
	    	//remove inserted number
	    	else if(v==digit_blank){
	    		chosenBtn.setText(" ");
	    		toggleWindow(digitPopup);
	    	}
	    	//remove popup by pressing the outside
	    	else if(v==digitHolder){
	    		toggleWindow(digitPopup);
	    	}
	    }
    }
}